package com.zhuang.framework.beans;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Classname MutablePropertyValues
 * @Description 封装和存储多个PropertyValue对象
 * @Date 2021/4/1 14:46
 * @Created by dell
 */

public class MutablePropertyValues implements Iterable<PropertyValue>{

    private final List<PropertyValue> propertyValueList;

    public MutablePropertyValues(){
        this.propertyValueList = new ArrayList<PropertyValue>();
    }

    public MutablePropertyValues(List<PropertyValue> propertyValueList){
        this.propertyValueList=(propertyValueList!=null ? propertyValueList : new ArrayList<PropertyValue>());
    }

    public PropertyValue[] getPropertyValues() {
        return this.propertyValueList.toArray(new PropertyValue[0]);
    }

    public PropertyValue getPropertyValue(String propertyName) {
        for (PropertyValue pv : this.propertyValueList) {
            if (pv.getName().equals(propertyName)) {
                return pv;
            }
        }
        return null;
    }

    @Override
    public Iterator<PropertyValue> iterator() {
        return propertyValueList.iterator();
    }

    //判断是否为空
    public boolean isEmpty(){
        return this.propertyValueList.isEmpty();
    }

    //添加属性值 判断是否已经有 有就覆盖属性值
    public MutablePropertyValues addPropertyValue(PropertyValue pv){
        for (int i = 0; i < propertyValueList.size(); i++) {
            PropertyValue currentPv = this.propertyValueList.get(i);
            if (currentPv.getName().equals(pv.getName())){
                this.propertyValueList.set(i, new PropertyValue(pv.getName(),pv.getRef(),pv.getValue()));
                return this;
            }
        }
        this.propertyValueList.add(pv);
        return this;
    }

    public boolean containsProperty(String propertyName){
        return getPropertyValues()!=null;
    }
}
